package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.ConstantType;
import his.entity.User;
import his.service.UserService;
import his.vo.DoctorVo;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/user/")
public class UserController {

    @Autowired
    UserService userService;


    @GetMapping("doctor/all")
    @ResponseBody
    public CommonResponse<List<DoctorVo>> getall_doctors()
    {
        return  userService.getall_doctors();
    }

    @GetMapping("doctor/search")
    @ResponseBody
    public CommonResponse<List<DoctorVo>> getall_doctors_byword(@RequestParam String keywords)
    {
        return  userService.getall_doctors_byword(keywords);
    }

    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<User>> getall_users()
    {
        return  userService.getall_users();
    }

    @PostMapping("add")
    @ResponseBody
    public CommonResponse<List<User>> add_user(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String userName=(String) jsonObject.get("userName");
        String password=(String) jsonObject.get("password");
        String RealName=(String) jsonObject.get("RealName");
        int useType=Integer.parseInt((String) jsonObject.get("useType"));
        int docTitleID=(int) jsonObject.get("docTitleID");
        String isScheduling=(String) jsonObject.get("isScheduling");
        int deptID=Integer.parseInt((String) jsonObject.get("deptID"));
        int registLeID=(int) jsonObject.get("registLeID");
        return  userService.add_user(userName,password,RealName,useType,docTitleID,isScheduling,deptID,registLeID);
    }

    @PostMapping("modify/{id_or_name}")
    @ResponseBody
    public CommonResponse<List<User>> update_users(@PathVariable String id_or_name,HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String userName=(String) jsonObject.get("userName");
        String password=(String) jsonObject.get("password");
        String RealName=(String) jsonObject.get("RealName");
        int useType=(int) jsonObject.get("useType");
        int docTitleID=(int) jsonObject.get("docTitleID");
        String isScheduling=(String) jsonObject.get("isScheduling");
        int deptID=(int) jsonObject.get("deptID");
        int registLeID=(int) jsonObject.get("registLeID");
        return  userService.update_users(id_or_name,userName,password,RealName,useType,docTitleID,isScheduling,deptID,registLeID);
    }

    @DeleteMapping ("del/{real_name}")
    @ResponseBody
    public CommonResponse<List<User>> delete_users(@PathVariable String real_name)
    {
        return  userService.delete_users(real_name);
    }

    @GetMapping ("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<User>> getall_users_byname(@PathVariable String keyword)
    {
        return  userService.getall_users_byname(keyword);
    }



}
